package com.epta.service;

import com.epta.common.AchievementStatus;
import com.epta.common.AchievementType;
import com.epta.common.CommonConst;
import com.epta.common.Role;
import com.epta.common.util.DateTimeUtil;
import com.epta.mapper.AchievementMapper;
import com.epta.pojo.Achievement;
import com.epta.pojo.Field;
import com.epta.pojo.Region;
import com.epta.pojo.User;
import com.epta.vo.AchievementBackDetailVo;
import com.epta.vo.AchievementBackListVo;
import com.epta.vo.AchievementFrontDetailVo;
import com.epta.vo.AchievementFrontListVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19 0019.
 */
@Service
public class AchievementService extends BaseService<Achievement> {
    @Autowired
    private AchievementMapper achievementMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private AuditLogService auditLogService;
    @Autowired
    private FieldService fieldService;
    @Autowired
    private RegionService regionService;

    /**
     * 获取后台列表
     */
    public List<AchievementBackListVo> queryBackList(Integer role) {
        List<Achievement> list = queryAll();
        List<AchievementBackListVo> backListVoList = new ArrayList<>();
        for (Achievement achievement : list) {
            backListVoList.add(assembleAchievementBackListVo(achievement));
        }
        return backListVoList;
    }

    private AchievementBackListVo assembleAchievementBackListVo(Achievement achievement) {
        AchievementBackListVo achievementBackListVo = new AchievementBackListVo();
        achievementBackListVo.setId(achievement.getId());
        achievementBackListVo.setTitle(achievement.getTitle());
        achievementBackListVo.setPrice(achievement.getPrice());
        achievementBackListVo.setTel(achievement.getTel());
        achievementBackListVo.setPatentee(achievement.getPatentee());
        achievementBackListVo.setVisitCount(achievement.getVisitCount());
        User user = userService.queryById(achievement.getUserId());
        if(user != null){
            achievementBackListVo.setUserName(user.getName());
            achievementBackListVo.setUserRole(Role.getValue(user.getRole()));
        }
        Field field = fieldService.queryById(achievement.getFieldId());
        if(field != null){
            achievementBackListVo.setField(field.getField());
        }
        Region region = regionService.queryById(achievement.getRegionId());
        if(region != null){
            achievementBackListVo.setRegion(region.getRegion());
        }
        achievementBackListVo.setType(AchievementType.getValue(achievement.getType()));
        achievementBackListVo.setStatus(AchievementStatus.getValue(achievement.getStatus()));
        achievementBackListVo.setBenefit(achievement.getBenefitStart() + "-" + achievement.getBenefitEnd());
        if (achievement.getSortOrder() > 0 && achievement.getSortOrder() <= CommonConst.MAX_SORT_ORDER) {
            achievementBackListVo.setStick("置顶");
        }
        return achievementBackListVo;
    }

}
